/*
 * Copyright 2010 Brian Watkins
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package bw.util;

import java.util.*;

public class DateFactory
{
    private static Object _object = new Object();
    private static DateFactory _instance = null;

    private TimeZone _timezone = null;

    public static DateFactory getInstance()
    {
        if ( _instance == null )
        {
            synchronized ( _object )
            {
                if ( _instance == null )
                {
                    _instance = new DateFactory();
                }
            }
        }

        return _instance;
    }

    private DateFactory ()
    {
        //nothing
    }

    public void setTimeZone ( String tz )
    {
        _timezone = TimeZone.getTimeZone( tz );
        bw.util.Log.getInstance().write( "Setting TimeZone to: " + _timezone.getDisplayName() );
    }

    public TimeZone getTimeZone ()
    {
        return _timezone;
    }

    public Date getDate ()
    {
        return getCalendar().getTime();
    }

    public Date getDateInTimeZone ( Date d )
    {
        return ( new Date( d.getTime() + _timezone.getRawOffset() ) );
    }

    public GregorianCalendar getCalendar ()
    {
        GregorianCalendar cal = new GregorianCalendar();
        cal.setTimeInMillis( System.currentTimeMillis() + _timezone.getRawOffset() );
        return cal;
    }

    public GregorianCalendar getCalendar ( int year, int month, int date )
    {
        GregorianCalendar cal = new GregorianCalendar( year, month, date );
        cal.setTimeZone( _timezone );

        return cal;
    }
}